﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using CinemaBookingUI.WCFReference;

namespace CinemaBookingUI
{
    public partial class CinemaRoom : Form
    {
        private WCFReference.IWcfCinemaService wcf = SingletonWCF.Instance.WCFNetTcp();
        private ShowTime shti;
        private Movie theMovie;
        private Booking booked;
        private int amountClicked;
        private List<Seat> selectedSeats;

        public CinemaRoom(Movie mov, ShowTime sht)
        {
            theMovie = mov;
            shti = sht;
            InitializeComponent();
            selectedSeats = new List<Seat>();

            lblMovieName.Text = theMovie.Title;
            cbIs3d.Checked = theMovie.Is3d;
            lblRoomNr.Text = shti.CinemaRoom.Id.ToString();

            List<Seat> seats = wcf.ReservedListGet(shti);


            CreateButtons();
            
            lblSelected.Text = amountClicked.ToString();
            double totalPrice = 0;
            foreach (Seat se in selectedSeats)
            {
                totalPrice += CalculatePrice(se);
            }
            lblCalculatedPrice.Text = totalPrice.ToString() + " kr";
        }

        private void btnReserve_Click(object sender, EventArgs e)
        {
            if (txtPhone.Text != "" && txtPhone.Text.Length == 8)
            {
                Insertion();
                #region MessageBox
                string message = "Bookingen er nu reserveret.\r\nKlik på ok for at gå til start skærmen.";
                string caption = "Resultat";
                MessageBoxButtons but = MessageBoxButtons.OK;
                DialogResult result;

                result = MessageBox.Show(message, caption, but);

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.Close();
                }
                #endregion
            }
            else
            {
                MessageBox.Show("Der mangler et telefon nummer!");
            }
        }

        private void btnSell_Click(object sender, EventArgs e)
        {
            if (txtPhone.Text != "" && txtPhone.Text.Length == 8)
            {
                Insertion();
                TicketInfo tic = new TicketInfo(booked);
                //tic.Parent = this;
                tic.Show();
            }
            else
            {
                MessageBox.Show("Der mangler et telefon nummer!");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (Seat se in selectedSeats)
            {
                wcf.ReservedListRemove(shti, se);
            }
            this.Close();
        }

        private double CalculatePrice(Seat seat)
        {
            return theMovie.Price + seat.Price + shti.Price;
        }

        private void Insertion()
        {
            Customer cust = wcf.CustomerObject(txtPhone.Text, txtName.Text, txtEmail.Text, null);
            booked = wcf.CreateBookingFromSeats(shti, selectedSeats, cust);
            cust.Booking = booked;
            foreach (Seat se in selectedSeats)
            {
                wcf.InsertTicket(wcf.TicketObject(CalculatePrice(se), shti, se, booked));
            }
        }

        private void CreateButtons()
        {
            List<Seat> seats = wcf.ReservedListGet(shti);
            seats.OrderBy(x => x.Row).ThenBy(x => x.SeatNr);
            int top = 35;
            int left = 2;
            List<Button> buttons = new List<Button>();

            #region Button create
            foreach (Seat se in seats)
            {
                Button but = new Button();
                but.Top = top;
                but.Left = left;
                but.Width = 30;
                but.Text = se.SeatNr.ToString();
                if (se.Handicap)
                {
                    but.BackColor = Color.DodgerBlue;
                }
                if (se.LegSpace)
                {
                    but.BackColor = Color.Gold;
                }
                if (se.IsTaken)
                {
                    but.BackColor = Color.Red;
                    but.Enabled = false;
                }
                #region Click Functions
                but.Click += (s, e) => 
                {
                    //but.Update();
                    //but.Refresh();
                    but.Invalidate();
                    //CreateButtons();
                    if (but.BackColor == Color.DarkGreen)
                    {
                        but.BackColor = Color.Empty;
                        wcf.ReservedListRemove(shti, se);
                        selectedSeats.Remove(se);
                        amountClicked--;
                    }
                    else
                    {
                        but.BackColor = Color.DarkGreen;
                        wcf.ReservedListAdd(shti, se);
                        selectedSeats.Add(se);
                        amountClicked++;
                        
                    }
                    //MessageBox.Show(se.SeatNr.ToString()); 
                    
                };
                #endregion
                buttons.Add(but);
                pSeats.Controls.Add(but);

                if (se.SeatNr % 10 == 0)
                {
                    Label lblEndRow = new Label();
                    lblEndRow.Left = left + 32;
                    lblEndRow.Top = top;
                    lblEndRow.Text = se.Row.ToString();
                    top += but.Height + 2;
                    left = 2;
                }
                else
                {
                    left += 32;
                }
            }
            #endregion
        }
    }
}
